package com.hex.liyue.giftmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.adapter.AdapterConfirmReceipt;
import com.hex.liyue.giftmodule.bean.HarverDetailsBean;
import com.hex.liyue.giftmodule.presenter.ConfirmReceiptPresenter;
import com.hex.liyue.giftmodule.presenter.IConfirmReceiptView;
import com.hex.liyue.usermodule.activity.AddressActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/28.
 * 确认收货界面
 */
public class ConfirmReceiptActivity extends BaseActivity implements IConfirmReceiptView, View.OnClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_shopping_list)
    YRecycleview rvShoppingList;


    TextView tvUsername;
    TextView tvAddress;
    TextView tvMobile;
    TextView tvErrorInfo;
    RelativeLayout rvDefaultAddress;
    private ConfirmReceiptPresenter confirmReceiptPresenter;
    private ArrayList gift_list;

    @Override
    protected void bindPresenter() {
        confirmReceiptPresenter = new ConfirmReceiptPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        View headView = View.inflate(this, R.layout.head_confirm_receipt, null);
        tvUsername = (TextView) headView.findViewById(R.id.tv_username);
        tvAddress = (TextView) headView.findViewById(R.id.tv_address);
        tvMobile = (TextView) headView.findViewById(R.id.tv_mobile);
        tvErrorInfo = (TextView) headView.findViewById(R.id.tv_error_info);
        rvDefaultAddress = (RelativeLayout) headView.findViewById(R.id.rv_default_address);
        rvDefaultAddress.setOnClickListener(this);
        rvShoppingList.addHeadView(headView);
        rvShoppingList.setLoadMoreEnabled(false);
        rvShoppingList.setReFreshEnabled(false);
        setAddress();//设置收貨地址

        gift_list = (ArrayList) getIntent().getSerializableExtra("gift_list");
        L.d("WJ", "==" + gift_list);
        AdapterConfirmReceipt adapterConfirmReceipt = new AdapterConfirmReceipt(gift_list);
        rvShoppingList.setLayoutManager(new LinearLayoutManager(this));
        rvShoppingList.setAdapter(adapterConfirmReceipt);
    }

    public void setAddress() {
        if (TextUtils.isEmpty(UserManager.getDeliveryId())) {
            tvErrorInfo.setVisibility(View.VISIBLE);
        } else {

            tvUsername.setVisibility(View.VISIBLE);
            tvAddress.setVisibility(View.VISIBLE);
            tvMobile.setVisibility(View.VISIBLE);
            tvUsername.setText("收货人：" + UserManager.getDeliveryUsername());
            tvAddress.setText("收货地址：" + UserManager.getDeliveryProvinceName() + " " + UserManager.getDeliveryCityName() + " " + UserManager.getDeliveryDistrictName() + " " + UserManager.getDeliveryAddressDetail());
            tvMobile.setText(UserManager.getDeliveryMobile());
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_confirm_receipt;
    }

    @Override
    protected void initToolbar() {
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("确认收货");
    }

    @OnClick({R.id.iv_toobar_back, R.id.tv_confirm_receipt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.rv_default_address:
                startAddressActivity();
                break;
            case R.id.tv_confirm_receipt:
                confirmReceipt();
                break;

        }
    }

    /**
     * 确认收货
     */
    private void confirmReceipt() {
        if (gift_list != null) {
            JSONArray jsonArray = new JSONArray(gift_list);
            confirmReceiptPresenter.gift_export(jsonArray);
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void startAddressActivity() {
        Intent intent = new Intent(this, AddressActivity.class);
        intent.putExtra("flag", true);//是收货地址过去的
        startActivityForResult(intent, 0);
    }

    @Override
    public void startHarvestGiftsDetailsActivity(HarverDetailsBean harverDetailsBean) {
        //进入收货详情
        L.d("WJ", "===dataBean-=" + harverDetailsBean.data.toString());
        Intent intent = new Intent(this, HarvestDetailsActivity.class);
        intent.putExtra("user_gift_export_id", harverDetailsBean.data.user_gift_export_id);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            //回来了
            tvErrorInfo.setVisibility(View.GONE);
            if (TextUtils.isEmpty(UserManager.getDeliveryId())) {
                tvErrorInfo.setVisibility(View.VISIBLE);
            } else {
                tvErrorInfo.setVisibility(View.GONE);
                tvUsername.setVisibility(View.VISIBLE);
                tvAddress.setVisibility(View.VISIBLE);
                tvMobile.setVisibility(View.VISIBLE);
                tvUsername.setText("收货人：" + UserManager.getDeliveryUsername());
                tvAddress.setText("收货地址：" + UserManager.getDeliveryProvinceName() + " " + UserManager.getDeliveryCityName() + " " + UserManager.getDeliveryDistrictName() + " " + UserManager.getDeliveryAddressDetail());
                tvMobile.setText(UserManager.getDeliveryMobile());
            }
            //可能收货地址会出问题
        }
    }

}
